//package org.cassandra;
//
//import cn.hutool.Hutool;
//import cn.hutool.core.io.FileUtil;
//import cn.hutool.poi.excel.ExcelFileUtil;
//import cn.hutool.poi.excel.ExcelReader;
//import cn.hutool.poi.excel.ExcelUtil;
//import cn.hutool.poi.excel.ExcelWriter;
//import com.alibaba.fastjson.JSONObject;
//import com.datastax.driver.core.TypeCodec;
//import com.datastax.oss.driver.api.core.CqlSession;
//import com.datastax.oss.driver.api.core.cql.ResultSet;
//import com.datastax.oss.driver.api.core.cql.Row;
//import lombok.AllArgsConstructor;
//import lombok.Data;
//import org.hyperic.sigar.Humidor;
//
//import java.net.InetSocketAddress;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//
//public class CassandraQueryTool {
//
//    private final CqlSession session;
//
//    public CassandraQueryTool(InetSocketAddress contactPoint, int port, String keyspace, String username, String password) {
//        this.session = CqlSession.builder()
//                .addContactPoint(contactPoint)
//                .withKeyspace(keyspace)
//                .withLocalDatacenter("datacenter1")
//                .withAuthCredentials(username, password)
//                .build();
//    }
//
//    public ResultSet executeQuery(String cqlQuery) {
//        return session.execute(cqlQuery);
//    }
//
//    public void close() {
//        session.close();
//    }
//
//    public static void main(String[] args) {
//        // Replace these values with your Cassandra cluster details
//        String ip = "172.21.11.36";
////        String ip = "ld-uf68jbw542qiiw534-proxy-lindorm-pub.lindorm.rds.aliyuncs.com";
//        int port = 9042;
//        String keyspace = "tyiotcloud";
//        String username = "cassandra";
//        String password = "cassandra";
//
//        CassandraQueryTool queryTool = new CassandraQueryTool(InetSocketAddress.createUnresolved(ip,port), port, keyspace, username, password);
//
//        Long startMill = 1693497600000L;
//
//        Long interval = 86400000L;
//
//        Long endMill = 0L;
//
//        Long now = System.currentTimeMillis();
//
//        List<DeviceAlarmInfo> alarmInfos = new ArrayList<>();
//
//        try{
//
//            List<DeviceInfo> infos = getDevice();
//            for (DeviceInfo device:infos) {
//                System.out.println("=======================wwww"+device.getDeviceId());
//                if(haveData(device,queryTool)){
//
//                    while (endMill<now){
//                        endMill = startMill+interval;
//                        alarmInfos.addAll(getTSData(device,queryTool,startMill,endMill));
//                        startMill = startMill+interval;
//                    }
//                }
//            }
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            // Close the session when done
//            queryTool.close();
//        }
//
//        ExcelWriter writer = ExcelUtil.getWriter(true);
//        // 设置 Excel 表头
//        writer.addHeaderAlias("deviceId", "设备Id");
//        writer.addHeaderAlias("deviceName", "设备名称");
//        writer.addHeaderAlias("status", "状态");
//        writer.addHeaderAlias("time", "时间");
//        writer.addHeaderAlias("value", "数据");
//
//
//        // 写入数据
//        writer.write(alarmInfos);
//        // 将 Excel 写入到文件
//        writer.setDestFile(FileUtil.file("C:\\Users\\leiyang\\Desktop\\devicealarm.xlsx"));
//        writer.flush();
//        writer.close();
//
//
//    }
//
//    public  static List<DeviceAlarmInfo>  getTSData(DeviceInfo deviceInfo , CassandraQueryTool tool, Long start , Long end){
//        String cqlQuery = "SELECT *  FROM ts_kv_cf WHERE entity_id =  %s AND entity_type ='DEVICE' AND key = '/tyiotcloud/radar/upRaw/ty00A0/%s/alarmRadar' AND ts>= %s and ts< %s  ALLOW FILTERING;";
//        ResultSet resultSet = tool.executeQuery(String.format(cqlQuery,deviceInfo.getDeviceId(),deviceInfo.getDeviceName(),start,end));
//        List<DeviceAlarmInfo> alarmInfos = new ArrayList<>();
//        for (Row row :resultSet) {
//            String str = row.getString("str_v");
//            Long ts = row.getLong("ts");
//            String status = JSONObject.parseObject(str).getString("status");
//            alarmInfos.add(new DeviceAlarmInfo(deviceInfo.getDeviceId(), deviceInfo.getDeviceName(), status,str,new Date(ts)));
//        }
//        System.out.println(JSONObject.toJSONString(alarmInfos));
//        return alarmInfos;
//    }
//
//    public  static  boolean haveData(DeviceInfo deviceInfo ,CassandraQueryTool tool){
//        String cqlQuery = "SELECT *  FROM ts_kv_latest_cf WHERE entity_id =  %s AND entity_type ='DEVICE' AND key = '/tyiotcloud/radar/upRaw/ty00A0/%s/alarmRadar' limit 1 ALLOW FILTERING;";
//        ResultSet resultSet = tool.executeQuery(String.format(cqlQuery,deviceInfo.getDeviceId(),deviceInfo.getDeviceName()));
//        return resultSet.one() != null;
//    }
//
//    public static List<DeviceInfo> getDevice(){
//        List<DeviceInfo> deviceId = new ArrayList<>();
////        ExcelReader reader = ExcelUtil.getReader("C:\\Users\\leiyang\\Desktop\\device\\2023-12-11-11-59-20_EXPORT_XLSX_11590772_586_0.xlsx","导出结果");
//                ExcelReader reader = ExcelUtil.getReader("C:\\Users\\leiyang\\Desktop\\device.xlsx","device");
//        for (int i=1;i<reader.getRowCount()-1;i++) {
//            deviceId.add(new DeviceInfo(reader.getCell(7, i).getStringCellValue(), reader.getCell(0, i).getStringCellValue())) ;
//        }
//        return deviceId;
//    }
//
//}
//
//
